package com.lcz.googleplay.widget;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.lcz.googleplay.R;
import com.lcz.googleplay.utils.UIUtils;

import butterknife.OnClick;

/**
 * Created by hp on 2016/10/26.
 */

public abstract class LoadingFrameLayout extends FrameLayout {
    //未知状态
    private int STATE_UNKOWN = 0;
    //加载中
    private int STATE_LOADING = 1;
    //错误
    private int STATE_ERROR = 2;
    //加载数据为空
    private int STATE_EMPTY = 3;
    //成功
    private int STATE_SUCCESS = 4;
    //标记
    private int state = STATE_UNKOWN;

    private View loadingView;
    private View errorView;
    private View emptyView;
    private View successView;

    public LoadingFrameLayout(Context context) {
        super(context);
        //创建4种加载状态加入FragmentLayout中
        init();
    }

    public LoadingFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        //创建4种加载状态加入FragmentLayout中
        init();
    }

    public LoadingFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //创建4种加载状态加入FragmentLayout中
        init();
    }

    /*创建4种加载状态加入FragmentLayout中*/
    private void init() {
        loadingView = createLoadingView();
        if (loadingView != null) {
            addView(loadingView,
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
        }

        errorView = createErrorView();
        if (errorView != null) {
            addView(errorView,
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
        }

        emptyView = createEmptyView();
        if (emptyView != null) {
            addView(emptyView,
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
        }

        showPage();
    }

    /*显示相应的页面*/
    private void showPage() {
        if (loadingView != null) {
            loadingView.setVisibility(state == STATE_UNKOWN || state == STATE_LOADING ? View.VISIBLE : View.INVISIBLE);
        }
        if (errorView != null) {
            errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE : View.INVISIBLE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE : View.INVISIBLE);
        }
        if (state == STATE_SUCCESS) {
            if (successView == null) {
                successView = createSuccessView();
                addView(successView,
                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT));
            }
            successView.setVisibility(View.VISIBLE);

        }
    }

    /*根据服务器返回数据切换状态*/
    public void show() {
        if (state == STATE_EMPTY || state == STATE_ERROR) {
            state = STATE_LOADING;
        }
        //请求服务器
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(200);
                final LoadResult result = load();
                UIUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result != null) {
                            state = result.getValue();
                            showPage();
                        }
                    }
                });
            }
        }).start();
        showPage();
    }

    @OnClick(R.id.page_bt)
    public void onClick() {
        show();
    }


    public enum LoadResult {
        error(2), empty(3), success(4);
        int value;

        LoadResult(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    private View createLoadingView() {
        View view = View.inflate(UIUtils.getActivity(), R.layout.loadpage_loading, null);
        return view;
    }

    private View createErrorView() {
        View view = View.inflate(UIUtils.getActivity(), R.layout.loadpage_error, null);
        return view;
    }

    private View createEmptyView() {
        View view = View.inflate(UIUtils.getActivity(), R.layout.loadpage_empty, null);
        return view;
    }

    /**
     * 返回加载成功的界面
     *
     * @return
     */
    public abstract View createSuccessView();

    /**
     * 请求服务器返回数据集结果
     *
     * @return
     */
    public abstract LoadResult load();

}
